<?
class FormParser
{
	var $retorno;
	var $formoptions;
	
	function FormParser()
	{
		global $OBXml;
		$this->formoptions = $OBXml->le(implode(file(AW_FS_INCLUDES."/confs/formdefaults.xml")));
		return true;
	}
	function form($field)
	{
		if(is_array($field))
		{
			$this->retorno = "";
			foreach(array_keys($field) as $key)
			{
				$this->retorno .= "<form ";
				$attributes = "";
				$atrarr = array();
				foreach(array_keys($field[$key]['attributes']) as $atrkey)
				{
					$attributes .= " $atrkey='".$field[$key]['attributes'][$atrkey]."' ";
				}
				$this->retorno .= "$attributes >";
				unset($field[$key]['attributes']);
				if(count($field[$key]) > 0)
				{
					$this->retorno .= $this->create_fields($field[$key]);
				}
				$this->retorno .= "</form>";
			}
			return $this->retorno;
		}
		else 
		{
			return false;
		}
	}
	
	function create_fields($field,$prefixo="",$values="")
	{
		global $smarty, $OBImagem, $OBColaboracao;
		if(is_array($field))
		{
			$retorno = "";
			foreach(array_keys($field) as $key)
			{
				if($field[$key]['attributes']['type'] == "mult")
				{
					if(!isset($values[$key]))
						$values[$key] = "";
					
					$mult = $this->create_mult($field[$key]['campos'],$prefixo."[".$key."]",$values[$key]);
					$smarty->assign("mult",$mult);
					$smarty->assign("attributes",$field[$key]['attributes']);
				}
				else 
				{
					if($prefixo != "")
					{
						$field[$key]['attributes']['name'] = $prefixo. "[" .$field[$key]['attributes']['name']. "]";
					}
					
					// pega imagem do BD
					if($field[$key]['attributes']['type'] == "imagem" && isset($OBImagem))
					{
						if(isset($values[$key]) && $values[$key] != "")
						{
							$imagem = $OBImagem->obtem("cod_imagem = '".intval($values[$key])."'");
							$field[$key]['attributes']['value'] = $values[$key];
						}
						else 
						{
							$imagem[0] = array('tipo_imagem' => '');
						}
						$smarty->assign("imagem",$imagem[0]);
					}
					
					// pega enquete do BD
					if($field[$key]['attributes']['type'] == "enquete" && isset($OBColaboracao))
					{
						if(isset($values[$key]) && $values[$key] != "")
						{
							$enquete = $OBColaboracao->obtem("cod_colaboracao = '".intval($values[$key])."'");
							$field[$key]['attributes']['value'] = $values[$key];
						}
						else 
						{
							$enquete[0] = array('cod_colaboracao' => '');
						}
						$smarty->assign("enquete",$enquete[0]);
					}
					
					if(isset($values[$key]))
					{
						$field[$key]['attributes']['value'] = $values[$key];
					}
					else
					{
						$field[$key]['attributes']['value'] = "";
					}
					// echo($key." -> ".$values[$key]."\n\n");
					
					$campo = $this->formoptions['formoptions'][$field[$key]['attributes']['type']];
					$campo['attributes'] = array_merge($campo['attributes'],$field[$key]['attributes']);
					if($field[$key]['attributes']['type'] == "select")
					{
						if(!isset($field[$key]['option']['attributes']['_num']))
						{
							$field[$key]['option'][0] = $field[$key]['option'];
						}
						else
						{
							unset($field[$key]['option']['attributes']['_num']);
						}
						$campo['option'] = array_merge($campo['option'],$field[$key]['option']);
						$smarty->assign("option",$campo['option']['attributes']);
					}
					
					// Caso seja editor html (fckeditor)
					if($field[$key]['attributes']['type'] == "fckeditor" && class_exists("FCKeditor"))
					{
						$oFCKeditor = new FCKeditor($field[$key]['attributes']['name']) ;
						$oFCKeditor->BasePath = $campo['attributes']['basepath'];
						$oFCKeditor->ToolbarSet = $campo['attributes']['toolbarset'] ;
						$oFCKeditor->Value = $field[$key]['attributes']['value'];
						$oFCKeditor->Height = $campo['attributes']['altura'];
						$oFCKeditor->Width = $campo['attributes']['largura'];
						$smarty->assign("fckeditor",$oFCKeditor->CreateHtml());
					}
					$smarty->assign("attributes",$campo['attributes']);
				}
				$retorno .= $smarty->fetch("sistema/campos/".$field[$key]['attributes']['type'].".tpl");
				$smarty->assign("attributes","");
				$smarty->assign("option","");
			}
			return $retorno;
		}
	}
	
	function create_mult($field,$prefixo="",$values="")
	{
		global $smarty;
		$mult = "";
		$i=0;
		$jsid = str_replace(array('[',']','_'),"",$prefixo);
		$smarty->assign("jsid",$jsid);
		if(is_array($values))
		{
			if(!isset($values[0]))
			{
				$tvalues = $values;
				unset($values);
				$values[0] = $tvalues;
				unset($tvalues);
			}
			if(isset($values['_num']))
				unset($values['_num']);
			
			foreach ($values as $value)
			{
				$value = $value;
				$value['ordem'] = $i;
				$mult .= "<fieldset id='fieldset_".$prefixo."_".$i."'>";
				$mult .= $this->create_fields($field,$prefixo."[$i]",$value);
				$mult .= '<div class="mult_controle">
<input type="button" name="up_'. $prefixo ."_". $i .'" value="&#8593" onclick="javascript:mult_up(\''. $prefixo .'\','. $i .',\''. $jsid .'\');"/><br />
<input type="button" name="down_'. $prefixo ."_". $i .'" value="&#8595" onclick="javascript:mult_down(\''. $prefixo .'\','. $i .',\''. $jsid .'\');"/><br />
<input type="button" name="del_'. $prefixo ."_". $i .'" value="x" onclick="javascript:mult_del(\''. $prefixo .'\','. $i .');"/></div>';
				$mult .= "</fieldset>";
				$i++;
			}
		}
		$value= array();
		$value['xmldata']['ordem'] = $i;
		
		$modelo = "<fieldset id='fieldset_".$prefixo."_%i%'>";
		$modelo .= $this->create_fields($field,$prefixo."[%i%]",$value);
		$modelo .= '<div class="mult_controle">
<input type="button" name="up_'. $prefixo .'_%i%" value="&#8593" onclick="javascript:mult_up(\''. $prefixo .'\',%i%,\''. $jsid .'\');"/><br />
<input type="button" name="down_'. $prefixo .'_%i%" value="&#8595" onclick="javascript:mult_down(\''. $prefixo .'\',%i%,\''. $jsid .'\');"/><br />
<input type="button" name="del_'. $prefixo .'_%i%" value="x" onclick="javascript:mult_del(\''. $prefixo .'\',%i%);"/></div>';
		$modelo .= "</fieldset>";
		
		$modelo = str_replace(array("\n","\r"),"",$modelo);
		$modelo = addslashes($modelo);
		
		$mult .= '
		<script type="text/javascript">
		'. $jsid ." = new Array();\n";
		
		$j=0;
		foreach(array_keys($field) as $k)
		{
			$mult .= $jsid ."[".$j."] = '".$prefixo."[%i%][$k]'\n";
			$j++;
		}
		
		$mult .= $jsid.'_tot = '.$i.';
		'. $jsid .'_modelo = \''.$modelo.'\';
		</script>
		';
		
		return $mult;
	}
}
?>